Retryer এর বাস্তব উদাহরণ এবং প্রয়োগ

Java Technologies - গুয়াভা (Guava) - Guava Retryer
208

গুয়াভা (Guava) লাইব্রেরি Java-তে অনেক কার্যকর utility এবং API সরবরাহ করে, যা প্রোগ্রামিংকে আরও সহজ এবং কার্যকর করে তোলে। গুয়াভার একটি গুরুত্বপূর্ণ ফিচার হল Retryer। এটি সাধারণত এমন ক্ষেত্রে ব্যবহৃত হয় যেখানে কোনো অপারেশন (যেমন API কল, ডেটাবেস অ্যাক্সেস) ব্যর্থ হলে পুনরায় চেষ্টা করার প্রয়োজন হয়।

এখানে Guava Retryer-এর একটি বাস্তব উদাহরণ এবং প্রয়োগ দেখানো হলো:


উদাহরণ: API কল পুনরায় চেষ্টা করা

ধরা যাক, আপনি একটি রিমোট API-তে কল করছেন, যা মাঝে মাঝে টাইমআউট বা ব্যর্থ হতে পারে। ব্যর্থ হলে, আপনি নির্দিষ্ট সময়ের ব্যবধানে পুনরায় চেষ্টা করতে চান।

Dependency (Maven)

<dependency>
    <groupId>com.github.rholder</groupId>
    <artifactId>guava-retrying</artifactId>
    <version>2.0.0</version>
</dependency>

কোড উদাহরণ

import com.github.rholder.retry.*;
import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;

public class GuavaRetryExample {

    public static void main(String[] args) {
        // Define the callable task
        Callable<Boolean> apiCall = () -> {
            System.out.println("Attempting API call...");
            // Simulate an API call that may fail
            if (Math.random() > 0.7) { // 30% chance of success
                System.out.println("API call succeeded!");
                return true;
            } else {
                System.out.println("API call failed. Retrying...");
                throw new RuntimeException("API call failed.");
            }
        };

        // Configure the Retryer
        Retryer<Boolean> retryer = RetryerBuilder.<Boolean>newBuilder()
                .retryIfException() // Retry if an exception is thrown
                .retryIfResult(result -> result == null || !result) // Retry if result is false
                .withWaitStrategy(WaitStrategies.fixedWait(2, TimeUnit.SECONDS)) // Wait 2 seconds between retries
                .withStopStrategy(StopStrategies.stopAfterAttempt(5)) // Stop after 5 attempts
                .build();

        try {
            // Execute the task with retry logic
            boolean result = retryer.call(apiCall);
            System.out.println("Final result: " + result);
        } catch (RetryException | ExecutionException e) {
            System.err.println("All retries failed: " + e.getMessage());
        }
    }
}

এই উদাহরণে কী হচ্ছে:

  1. Callable Task: apiCall একটি Callable হিসাবে সংজ্ঞায়িত করা হয়েছে যা API কল করে। এটি ব্যর্থ হলে RuntimeException ছুঁড়ে দেয়।
  2. Retryer Configuration:
    • ব্যর্থ হলে পুনরায় চেষ্টা করা হবে।
    • প্রতিবার ২ সেকেন্ড অপেক্ষা করবে।
    • সর্বাধিক ৫ বার চেষ্টা করবে।
  3. Execution: retryer.call(apiCall) API কলটি পুনরায় চালায় যতক্ষণ না এটি সফল হয় বা সর্বাধিক সীমা অতিক্রম করে।

বাস্তব প্রয়োগের ক্ষেত্রে এটি কোথায় উপকারী:

  • API Retry: বহিরাগত API কল ব্যর্থ হলে পুনরায় চেষ্টা করা।
  • Database Query: কোনো ডেটাবেস query টাইমআউট বা deadlock হলে পুনরায় চেষ্টা করা।
  • File Upload/Download: ফাইল স্থানান্তর ব্যর্থ হলে পুনরায় চেষ্টা করা।

গুরুত্বপূর্ণ বিষয়:

  • WaitStrategies এবং StopStrategies কাস্টমাইজ করে পুনরায় চেষ্টার সময় এবং সীমা নিয়ন্ত্রণ করা যায়।
  • RetryException ব্যবহার করে ব্যর্থতার কারণ এবং সমস্ত প্রচেষ্টার ফলাফল ডিবাগ করা যায়।

এই পদ্ধতি আপনার অ্যাপ্লিকেশনে ফ্রগাইল কাজগুলোকে আরও রেজিলিয়েন্ট করে তুলতে সাহায্য করে।

Content added By
Promotion
NEW SATT AI এখন আপনাকে সাহায্য করতে পারে।

Are you sure to start over?

Loading...